<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Backend;
use hg\apidoc\annotation as Apidoc;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

/**
 * 邮件发送
 * @Apidoc\Group("base")
 * @package app\admin\controller
 */
class Email extends Backend
{
    /**
     * 初始化参数
     * @author 贾二小
     * @since 2021/7/3
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 邮件发送
     * @author 贾二小
     * @since 2021/7/3
     */
    public function send()
    {
        //参数
        $to = input('to');//发送人
        $title = input('title');//标题
        $content = input('content');//内容
        //发送邮件
        $mail = new PHPMailer(true);
        try {
            $mail->CharSet= PHPMailer::CHARSET_UTF8;
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_OFF;
            $mail->isSMTP();
            $mail->Host       = get_config('email_host');
            $mail->SMTPAuth   = true;
            $mail->Username   = get_config('email_username');
            $mail->Password   = get_config('email_password');
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->Port       = get_config('email_port');
            $mail->setFrom(get_config('email_from'));
            foreach ($to as $vo){
                $mail->addAddress($vo);
            }
            //Content
            $mail->isHTML(true);
            $mail->Subject = $title;
            $mail->Body    = $content;
            $mail->send();
        } catch (Exception $e) {
            $this->error($mail->ErrorInfo);
        }
        //日志
        $this->sLog('邮件发送_发送',"成功");
        //返回
        $this->success();
    }

}